package com.example.myzhujielearing;

import androidx.annotation.BinderThread;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.bottonknife.ButtonKnife;
import com.example.compiler.BindView;
import com.example.compiler.MyAnnotion;
import com.example.compiler.ViewBind;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * gradel 5.1之上 看compiler中的gradle 中的implementation与annotationProcessor都要
 * implementation 'com.google.auto.service:auto-service:1.0-rc2'
 * annotationProcessor  'com.google.auto.service:auto-service:1.0-rc2'
 */

@MyAnnotion(id = "1", value = "注解")
public class MainActivity extends AppCompatActivity {

    @ViewBind(R.id.tv_1)
    public TextView textView;

    @ViewBind(R.id.tv_2)
    public TextView textView2;

    @BindViewRuning(R.id.bt_1)
    public Button btStartSecond;

    @BindViewRuning(R.id.bt_1)
    public Button btStartSecond2;

    @BindViewRuning(R.id.bt_1)
    public Button btStartSecond3;
    @BindViewRuning(R.id.bt_1)
    public Button btStartSecond4;
    @BindViewRuning(R.id.bt_1)
    public Button btStartSecond5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        long time = System.currentTimeMillis();
        Log.d("tgw运行时开始注解时间", "reflectNewInstance book = " +  (System.currentTimeMillis()));
        inject(this);
        Log.d("tgw运行时结束注解时间", "reflectNewInstance book = " +  (System.currentTimeMillis()));
        reflectNewInstance();
        time = System.currentTimeMillis();
        ButtonKnife.bind(this);
        Log.d("tgw编译时时间", "reflectNewInstance book = " +  (System.currentTimeMillis()-time));
        textView.setText("绑定的注解");
        textView2.setText("绑定的注解textView2");
        btStartSecond.setText("运行时注解跳转");
//        btStartSecond.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startActivity(new Intent(MainActivity.this,SecondActivity.class));
//            }
//        });
        injectStartClick(this);

    }

    public  void inject(Activity activity) {
        try {
            Field[] fields = activity.getClass().getDeclaredFields();
            if (fields != null && fields.length > 0) {
                for (Field field : fields) {
                    BindViewRuning viewInject = field.getAnnotation(BindViewRuning.class);
                    if (viewInject != null) {
                        int viewId = viewInject.value();
                        try {
                            field.setAccessible(true);
                            field.set(activity, activity.findViewById(viewId));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }catch (Exception e){

        }

    }

    // 创建对象
    public static void reflectNewInstance() {
        try {
            Class<?> classBook = Class.forName("com.example.demoprocessor.auto.Tgwlieming$$ZYAO");

            Method methodBook = classBook.getDeclaredMethod("getMessage");
            methodBook.setAccessible(true);
            Object objectBook = classBook.newInstance();
            String string = (String) methodBook.invoke(objectBook);
            Log.d("tgw注解", "reflectNewInstance book = " + string);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    /**
     * 参考 ：https://www.jianshu.com/p/9be58ee20dee
     * https://blog.csdn.net/dapan728/article/details/65648740
     * */
    @BindClickRuning(value = R.id.bt_1)
    public void setBtStartSecond(View view){
        startActivity(new Intent(MainActivity.this,SecondActivity.class));
    }

    public  void injectStartClick(final Activity activity) {
        try {
            Method[] methods = activity.getClass().getDeclaredMethods();
            if (methods != null && methods.length > 0) {
                for (final Method method : methods) {
                    BindClickRuning onClick = method.getAnnotation(BindClickRuning.class);
                    if (onClick != null) {
                        int viewId = onClick.value();
                        try {
                            method.setAccessible(true); //这样就不需要强制用户将方法设置为public权限了
                            final View view =  activity.findViewById(viewId);
                            view.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    try {
                                        //方法调用，method为activity对象中的方法（注解修饰）， activity为对象 view为参数
                                        method.invoke(activity,view);
                                    } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                    } catch (InvocationTargetException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }catch (Exception e){

        }

    }


}
